package com.example.sunsg.okhttptest;

import android.app.Activity;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.breadtrip.R;
import com.example.sunsg.okhttptest.Manager.BinderPollManager;
import com.example.sunsg.okhttptest.bean.Book;
import com.example.sunsg.okhttptest.bean.User;

/**
 * binder pool
 */

public class BinderPoolManagerActivity extends Activity implements View.OnClickListener{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_binder_pool_manager);
        Button btnBind = ((Button) findViewById(R.id.btn_bind));
        btnBind.setOnClickListener(this);
        Button btnUnbind = ((Button) findViewById(R.id.btn_unbind));
        btnUnbind.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_bind:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        doWork();
                    }
                }).start();
                break;
            case R.id.btn_unbind:

                break;
        }
    }
    private void doWork(){
        BinderPollManager bindmanger = BinderPollManager.getInstance(this);
        IBinder binder = bindmanger.queryBinder(BinderPollManager.IMYAIDLINTERFACE);
        IMyAidlInterface normalBind = (IMyAidlInterface) binder;
        try {
            User user = normalBind.getUser();
            Log.i("test","uesr = "+user.toString());
        } catch (RemoteException e) {
            e.printStackTrace();
        }

        IBinder binder1 = bindmanger.queryBinder(BinderPollManager.IMYAIDLINTERFACE_ADD_BOOK);
        IMyAidlInterfaceAddBook addBookBind = (IMyAidlInterfaceAddBook) binder1;
        try {
            addBookBind.addBook(new Book("susng",12));
            addBookBind.addBook(new Book("susng22",22));
            String tostrint = addBookBind.getBooks().toString();
            Log.i("test","books = "+tostrint);

        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}
